﻿Ext.define("PhaseProjectModel", {
    extend: "Ext.data.Model",
    fields: []
});

Ext.define('app.prm.project.PhaseProject', {
    extend: 'Ext.panel.Panel',
    border: false,
    layout: 'fit',
    initComponent: function () {

        var me = this;
        me.items = [me.getGrid()];

        me.callParent();
    },
    listeners: {
        afterrender: function (me) {
            App.AjaxGet('/api/services/app/project/GetHistoryProjectPhase', function (data) {
                console.log(data);
                var yearPhase = data.result.yearPhase;
                var phaseLst = data.result.phaseLst;

                var columns = [];
                var fields = [];
                columns.push({ header: '年份', dataIndex: 'year', locked: true });
                fields.push('year');
                for (var i = 0; i < phaseLst.length; i++) {
                    columns.push({
                        header: phaseLst[i].name, dataIndex: phaseLst[i].id + "Field", renderer: function (value, metaData, record, rowIndex) {
                            if (value != undefined && value != '') {
                                return record.get(value + 'Total');
                            }
                        }
                    });
                    fields.push(phaseLst[i].id + "Field");
                    fields.push(phaseLst[i].id + 'Total');
                    fields.push(phaseLst[i].id + 'Finish');
                }
                PhaseProjectModel.removeFields(PhaseProjectModel.fields);
                PhaseProjectModel.addFields(fields);
                var store = new Ext.data.JsonStore({
                    fields: fields
                });

                for (var i = yearPhase.length - 1; i >= 0; i--) {
                    var item = yearPhase[i];
                    var r = store.findRecord('year', item.year)
                    if (r == null) {
                        r = Ext.create("PhaseProjectModel", { year: item.year });
                        store.add(r);
                    }
                    r.set(item.phaseId + 'Field', item.phaseId);
                    r.set(item.phaseId + 'Total', item.proCount);
                    r.set(item.phaseId + 'Finish', item.proFCount);
                }

                store.commitChanges();
                me.getGrid().reconfigure(store, columns);
            });
        }
    },
    getGrid: function () {
        if (!this.grid) {
            this.grid = Ext.create('Ext.grid.Panel', {
                border: false,
                enableLocking: true,
                columns: [],
                columnLines: true,
                listeners: {
                    'celldblclick': function (table, td, cellIndex, record, tr, rowIndex, e, eOpts) {

                        var fieldName = table.panel.headerCt.getHeaderAtIndex(cellIndex).dataIndex;
                        var phaseTxt = table.panel.headerCt.getHeaderAtIndex(cellIndex).text;
                        if (fieldName.indexOf('Field') > 0) {

                            var phase = record.get(fieldName);
                            var year = record.get('year');
                            var t = record.get(phase + 'Total');
                            if (t > 0)
                            {
                                Ext.create('app.prm.project.YearPhaseProjectView', { title: year + ' ' + phaseTxt, phaseId: phase, year: year }).show();
                            }
                            
                        }
                    }
                }
            });
        }
        return this.grid;
    }
});